package com.sinosoft.cloud.dp.service;

import com.alibaba.fastjson.JSON;
import com.sinosoft.cloud.dp.thread.BusinessService;
import com.sinosoft.utility.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class ReadTxtService implements BusinessService {
    Log log = LogFactory.getLog(ReadTxtService.class);
    public static final int CONTAINER_COUNT = 100;//分片的容量，即每个线程体处理的数据量
    private String one_set_key = "one_set";
    private String txt_url;
    private FileInputStream fileInputStream = null;
    private Scanner scanner = null;

    public ReadTxtService(String txt_url) {
        this.txt_url = txt_url;
    }

    @Override
    public void prepareElement() {
        //准备数据，读取txt

        try {
            fileInputStream = new FileInputStream(txt_url);
            scanner = new Scanner(fileInputStream, "UTF-8");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.error("文件" + txt_url + "，Not Found");
            log.error(ExceptionUtils.exceptionToString(e));
            return;
        } catch (IOException e) {
            log.error("读取文件" + txt_url + "，出现IO异常");
            log.error(ExceptionUtils.exceptionToString(e));
            return;
        } finally {

        }

    }

    //判断prepareDate里面是否还有未取完的数据
    @Override
    public boolean hasMoreElement() {
        if (scanner == null) {
            return false;
        }
        if(!scanner.hasNext()){
            return false;
        }
        return true;
    }

    //数据分片
    @Override
    public HashMap nextElement() {
        HashMap hashElement = new HashMap();
        if (scanner != null) {
                int tCount = 0;
                ArrayList<String> one_set = new ArrayList<>();
                while (scanner.hasNext()) {
                    one_set.add(scanner.nextLine());
                    //分片容器加载完成，终止循环
                    if (++tCount == CONTAINER_COUNT)
                        break;
                    //txt数据读取完成，终止循环
                    if (!scanner.hasNext())
                        break;
                }
                hashElement.put(one_set_key, one_set);
        }
        return hashElement;
    }

    @Override
    public HashMap processOneElement(HashMap aElement) {
        ArrayList<String> dataSet = (ArrayList<String>) aElement.get(one_set_key);
        HashMap hashResult = new HashMap();

        log.info("当前线程：：" + Thread.currentThread().getName() + "，分片大小：：" + dataSet.size());
        log.info("分片数据：：" + JSON.toJSONString(dataSet));
        return hashResult;
    }

    @Override
    public boolean saveOneElement(HashMap aElement) {
        return false;
    }

    @Override
    public boolean flushAllElements() {
        return false;
    }

    @Override
    public void onInitialiseResource() {
        log.info("初始化资源成功");
    }

    @Override
    public void onReleaseResource() {
        //关闭资源

        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(scanner != null){
            scanner.close();
        }

    }

    @Override
    public void dealException() {

    }

    @Override
    public void setExceptionFlag(boolean bExceptionFlag) {

    }

    @Override
    public boolean getExceptionFlag() {
        return false;
    }
}
